/*
 * Copyright 2006-2022 ahtelit.com, All Rights Reserved.
 */

package com.telit.firecommand.controller;

import com.telit.common.constant.Constant;
import com.telit.common.entity.DataReturn;
import com.telit.common.entity.PageData;
import com.telit.firecommand.model.sysUser.UpdateUserRequest;
import com.telit.firecommand.service.SysUserService;
import com.telit.firecommand.vo.ForesterVO;
import com.telit.firecommand.vo.MicroSysUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 系统用户API
 *
 * @author yuezhuang
 * @since 2023-01-17
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping(value = "/sys-user", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(tags = "系统用户API", value = "系统用户API", produces = MediaType.APPLICATION_JSON_VALUE)
public class SysUserController {

    private final SysUserService sysUserService;

    /**
     * 更新用户信息
     *
     * @param updateUserRequest 请求参数
     *
     * @return DataReturn
     */
    @ApiOperation(httpMethod = Constant.REQUEST_METHOD_POST, value = "更新用户信息")
    @PostMapping(path = "/update")
    public DataReturn<MicroSysUserVO> update(@RequestBody UpdateUserRequest updateUserRequest) {
        return DataReturn.ok(sysUserService.update(updateUserRequest));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户真实姓名", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "onlineStatus", value = "在线状态 0-离线 1-在线", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "regionCode", value = "所属行政区划编码", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "areaRecursion", value = "是否包含子行政区划数据 true/false， 默认 false", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "roleCodes", value = "角色编码（使用英文逗号拼接）", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "ids", value = "id（使用英文逗号拼接）", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "current", value = "当前页数", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", required = true, paramType = "query", dataType = "int"),
    })
    @GetMapping(value = "/qry-list", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "分页查询列表", httpMethod = Constant.REQUEST_METHOD_GET)
    public DataReturn<PageData<List<ForesterVO>>> qryList(@RequestParam(required = false) String name,
                                                          @RequestParam(required = false) Integer onlineStatus,
                                                          @RequestParam(value = "regionCode", required = false) String regionCode,
                                                          @RequestParam(value = "areaRecursion", required = false, defaultValue = "false") boolean areaRecursion,
                                                          @RequestParam(value = "roleCodes", required = false) String roleCodes,
                                                          @RequestParam(value = "ids", required = false) String ids,
                                                          @RequestParam(value = "current", defaultValue = "1") Integer current,
                                                          @RequestParam(value = "size", defaultValue = "20") Integer size) {
        return DataReturn.ok(
                sysUserService.qryList(onlineStatus, name, regionCode, areaRecursion, roleCodes, ids, current, size));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户真实姓名", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "onlineStatus", value = "在线状态 0-离线 1-在线", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "regionCode", value = "所属行政区划编码", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "areaRecursion", value = "是否包含子行政区划数据 true/false， 默认 false", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "roleCodes", value = "角色编码（使用英文逗号拼接）", paramType = "query", dataType = "String"),
    })
    @GetMapping(value = "/qry-id-list", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询Id列表", httpMethod = Constant.REQUEST_METHOD_GET)
    public DataReturn<List<Long>> qryIdList(@RequestParam(required = false) String name,
                                            @RequestParam(required = false) Integer onlineStatus,
                                            @RequestParam(value = "regionCode", required = false) String regionCode,
                                            @RequestParam(value = "areaRecursion", required = false, defaultValue = "false") boolean areaRecursion,
                                            @RequestParam(value = "roleCodes", required = false) String roleCodes) {
        return DataReturn.ok(sysUserService.qryIdList(onlineStatus, name, regionCode, areaRecursion, roleCodes));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query", dataType = "String"),
    })
    @GetMapping(value = "/detail/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询详情", httpMethod = Constant.REQUEST_METHOD_GET)
    public DataReturn<ForesterVO> qryDetail(@PathVariable(value = "id") Long id) {
        return DataReturn.ok(sysUserService.qryDetail(id));
    }

}
